<template>
  <view class="box">
    <view class="title" v-if="showTitle">
      {{ title }}
    </view>
    <view
      class="box_item"
      v-for="(item, index) in listv"
      @click="toDetail(item)"
    >
      <view class="item_radio" v-show="!flag">
        <view class="item_left_clickView" @click.stop="change(item)">
          <image
            v-if="!item.clickable"
            class="item_left_click"
            src="../../static/images/my/icon_grzy_quanxuan.png"
            mode=""
          ></image>
          <image
            v-else
            class="item_left_clickable"
            src="../../static/images/my/icon_grzy_xc_xuanzhong.png"
            mode=""
          ></image>
        </view>
      </view>
      <view class="item_content">
        <view class="item_head">
          <view class="head_left">
            <image
              src="../../static/images/icon_tllb_taolun.png"
              mode=""
              v-if="!whiteColor"
            ></image>
            <image
              src="../../static/images/icon_sy_xiaoxi.png"
              mode=""
              v-if="whiteColor"
            ></image>
          </view>
          <view class="head_right" :class="whiteColor ? 'head_right1' : ''">
            <view class="right_text">
              {{ item.title }}
            </view>
            <view class="right_label" v-if="item.is_zhiding != 0"> 置顶 </view>
            <view class="right_label label1" v-if="item.is_zhiding != 0">
              精华
            </view>
            <view
              class="right_action"
              @click.stop="action(item)"
              v-if="(shenfen == 1 || shenfen == 2) || userId == item.user_id"
            >
              <u-icon name="more-dot-fill" color="#000" size="22"></u-icon>
              <!-- {{item.isOpen}} -->
              <view class="operate" v-if="item.isOpen ">
                <view
                  class="operate_item"
                  v-if="shenfen == 1 || (userId == item.user_id)"
                  @click.stop="del(item)"
                >
                  删除帖子
                </view>
                <view
                  class="operate_item"
                  v-if="shenfen == 1 || (userId == item.user_id)"
                  @click.stop="goBianji(item)"
                >
                  编辑帖子
                </view>
                <view
                  class="operate_item"
                  v-if="shenfen == 2 && item.is_zhiding != 0"
                  @click="quxiao(item)"
                >
                  取消置顶精华
                </view>
                <view
                  class="operate_item"
                  v-if="shenfen == 2 && item.is_zhiding == 0"
                  @click="zhiding(item)"
                >
                  置顶精华
                </view>
              </view>
            </view>
          </view>
        </view>
        <view class="item_bom">
          <view class="bom_left" :class="whiteColor ? 'bom_left1' : ''">
            {{ item.comment_count }}
          </view>
          <view
            class="bom_right"
            :class="whiteColor ? 'bom_right1' : ''"
            v-if="item.user"
          >
            <image
              :src="item.user.avatar_url"
              mode=""
              @click.stop="goHome(item.user.user_id)"
            ></image>
            <text>{{ item.user.nick_name }}</text>
            <text class="bom_right_time">{{ item.create_time }}</text>
          </view>
        </view>
      </view>
    </view>

    <view class="bottom" v-show="!flag">
      <view class="bottom_left">
        <view @click="changeClick">
          <image
            v-if="!clickableAll"
            class="item_left_click"
            src="../../static/images/my/icon_grzy_quanxuan.png"
            mode=""
          ></image>
          <image
            v-else
            class="item_left_click"
            src="../../static/images/my/icon_grzy_xc_xuanzhong.png"
            mode=""
          ></image>
        </view>
        <view class="bottom_left_text">全选</view>
      </view>

      <view class="bottom_right" @click="deleteFn">删除</view>
    </view>

    <u-modal
      :show="show"
      :title="title1"
      :content="content"
      confirmColor="#55B877"
      :closeOnClickOverlay="true"
      @confirm="confirm"
      @close="close"
      @cancel="cancel"
      :showCancelButton="true"
    ></u-modal>

    <view
      class="empty_view"
      v-if="listv && listv.length == 0"
      :style="{
        marginTop: title == '内容列表' ? '100rpx' : '300rpx',
      }"
    >
      <image
        class="item_left_clickable"
        src="../../static/images/my/kong.png"
        mode=""
      ></image>
      <view v-if="from == 'history'">您还没有历史记录~</view>
      <view v-else>当前没有相关数据，请查看其他分类或功能。</view>
    </view>
  </view>
</template>

<script>
import {
  createDiscuss,
  delete_discuss,
  discuss_zhiding,
  quxiao_discuss_zhiding,
} from "@/common/api/start.js";
export default {
  props: {
    title: {
      type: String,
      default: "讨论列表",
    },
    showTitle: {
      type: Boolean,
      default: true,
    },
    whiteColor: {
      type: Boolean,
      default: false,
    },
    flag: {
      type: Boolean,
      default: true,
    },
    list: {
      type: Array,
      default: () => {
        return [];
      },
    },
    userId:{
      type: Number,
      default: 0,
    },
    type: {
      type: String,
      default: "",
    },
    shenfen: {
      type: Number,
      default: 0,
    },
    xingheId: {
      type: Number,
      default: 0,
    },
    from: {
      type: String,
      default: "",
    },
    zu:{
      type: String,
      default: "",
    }
  },
  watch: {
    list: {
      deep: true, // 深度监听每一个属性值, 如果是对象可以深度监听
      immediate: true, // 进入页面后立即自动调用handler函数
      handler(newValue) {
        this.listv = newValue;
        if (this.listv) {
          for (let i = 0; i < this.listv.length; i++) {
            this.listv[i].clickable = false;
            console.log("tttttttt");
            this.listv[i].isOpen = false;
          }
        }

        console.log(this.listv, "newList");
      },
    },
  },
  data() {
    return {
      list2: [
        {
          num: "44",
          title: "品牌在历史上也有着重要的三次改变888",
          author: "冰释老师",
          time: "1小时前更新",
          url: "",
          clickable: false, // 当前是否选中
        },
        {
          num: "49",
          title: "品牌在历史上也有着重要的三次改变888",
          author: "冰释老师",
          time: "1小时前更新",
          url: "",
          clickable: false, // 当前是否选中
        },
        {
          num: "14",
          title: "品牌在历史上也有着重要的三次改变888",
          author: "冰释老师",
          time: "1小时前更新",
          url: "",
          clickable: false, // 当前是否选中
        },
        {
          num: "84",
          title: "品牌在历史上也有着重要的三次改变888",
          author: "冰释老师",
          time: "1小时前更新",
          url: "",
          clickable: false, // 当前是否选中
        },
      ],
      clickableAll: false,
      show: false,
      title1: "确认删除",
      content: "确认删除选中内容？",
      listv: [],
    };
  },
  methods: {
    // 去个人主页
    goHome(user_id) {
      uni.navigateTo({
        url: `/pages/home/home?user_id=${user_id}`,
      });
    },
    toDetail(item) {
      // if(this.type == 'start') {
      // 	uni.navigateTo({
      // 	  url: "/pages/distinguish/detail?id=" + item.id,
      // 	});
      // } else {
      // 	uni.navigateTo({
      // 	  url: "/pages/detail/detail",
      // 	});
      // }
      uni.navigateTo({
        url:
          "/pages/distinguish/detail?id=" +
          item.id +
          "&xinghe_id=" +
          this.xingheId,
      });
    },
    change(item) {
      this.$forceUpdate();
      item.clickable = !item.clickable;
      // 判断是否全选
      let all = true;
      for (let i = 0; i < this.listv.length; i++) {
        if (this.listv[i].clickable == false) {
          all = false;
        }
      }
      if (all) {
        this.clickableAll = true;
      } else {
        this.clickableAll = false;
      }
    },
    changeClick() {
      this.clickableAll = !this.clickableAll;
      if (this.clickableAll) {
        for (let i = 0; i < this.listv.length; i++) {
          this.listv[i].clickable = true;
        }
      } else {
        for (let i = 0; i < this.listv.length; i++) {
          this.listv[i].clickable = false;
        }
      }
    },
    confirm() {
      let arr = [];
      let num = 0;
      for (let i = 0; i < this.listv.length; i++) {
        if (this.listv[i].clickable == true) {
          arr.push(this.listv[i].history_id);
          num++;
        }
      }
      console.log(arr, "数组");
      this.deletFn(arr);
    },
    close() {
      this.show = false;
    },
    cancel() {
      this.show = false;
    },
    // 删除按钮
    deleteFn() {
      let isTrue = true;
      for (let i = 0; i < this.listv.length; i++) {
        console.log(this.listv[i].clickable);
        if (this.listv[i].clickable == true) {
          isTrue = false;
        }
      }
      if (isTrue == true) {
        uni.$u.toast("您还未选择内容");
      } else {
        this.show = true;
      }
    },
    // 删除接口
    deletFn(arr) {
      this.$http
        .historyDel_his_gongchuang({ ids: arr })
        .then((res) => {
          uni.$u.toast(res.message);
          this.show = false;
          setTimeout(() => {
            this.$emit("updateDataGongXing");
          }, 1000);
        })
        .catch((error) => {
          uni.$u.toast(error);
        });
    },
    del(item) {
      const i = this.listv.findIndex((val) => {
        return val.id == item.id;
      });
      this.listv[i].isOpen = false;
      delete_discuss({
        id: item.id,
        xinghe_id: this.xingheId,
      })
        .then((res) => {
          this.listv.splice(i, 1);
          uni.$u.toast("删除成功");
        })
        .catch((err) => {
          uni.$u.toast(err);
        });
      this.$forceUpdate();
    },
    goBianji(item){
      console.log(item,'当前项')
      uni.navigateTo({
        url: "/pages/distinguish/editDiscuss?id=" +
          item.id +
          "&zu=" +
          this.zu 
      });
    },
    zhiding(item) {
      console.log("iiiitem置顶了", item);
      const i = this.listv.findIndex((val) => {
        return val.id == item.id;
      });
      this.listv[i].isOpen = false;
      discuss_zhiding({
        discuss_id: item.id,
        xinghe_id: this.xingheId,
      })
        .then((res) => {
          this.listv[i].is_zhiding = 1;
          uni.$u.toast("置顶成功");
          this.$emit("refresh", 1);
        })
        .catch((err) => {
          uni.$u.toast("置顶失败");
        });
      this.$forceUpdate();
    },
    quxiao(item) {
      const i = this.listv.findIndex((val) => {
        return val.id == item.id;
      });
      this.listv[i].isOpen = false;
      quxiao_discuss_zhiding({
        discuss_id: item.id,
        xinghe_id: this.xingheId,
      })
        .then((res) => {
          this.listv[i].is_zhiding = 0;
          uni.$u.toast("取消置顶成功");
          this.$emit("refresh", 1);
        })
        .catch((err) => {
          uni.$u.toast("取消置顶失败");
        });
      this.$forceUpdate();
    },
    action(item) {

      
      const i = this.listv.findIndex((val) => {
        return val.id == item.id;
      });

      this.listv.forEach((item) => {
        item.isOpen = false;
      });

      if (!item.isOpen) {
        this.listv[i].isOpen = true;
        this.$forceUpdate();
      }

      if(this.userId == item.user_id){
        this.listv[i].isOpen = true;
      }
      console.log("111111", this.listv, this.listv[i].isOpen);
    },
  },
};
</script>

<style scoped lang="scss">
.box {
  width: 100%;
  margin-top: 40rpx;
  .title {
    font-size: 28rpx;
    font-family: PingFang SC-Medium, PingFang SC;
    font-weight: 400;
    color: #444444;
    margin-bottom: 25rpx;
  }
  .box_item {
    width: 100%;
    display: flex;
    align-items: center;
    .item_radio {
      .item_left_clickView {
        margin-right: 24rpx;
        image {
          position: relative;
          bottom: 20rpx;
        }
        .item_left_click {
          width: 40rpx;
          height: 40rpx;
        }
        .item_left_clickable {
          width: 40rpx;
          height: 40rpx;
        }
      }
    }
    .item_content {
      flex: 1;
    }
    .item_head {
      width: 100%;
      display: flex;
      .head_left {
        width: 48rpx;
        height: 48rpx;
        image {
          width: 100%;
          height: 100%;
        }
      }
      .head_right {
        flex: 1;
        padding-left: 30rpx;
        box-sizing: border-box;
        font-size: 30rpx;
        font-family: PingFang SC-Medium, PingFang SC;
        font-weight: 400;
        color: #444444;

        display: flex;
        position: relative;
        .right_text {
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
        }
        .right_label {
          width: 68rpx;
          height: 36rpx;
          background: rgba(255, 209, 153, 0.302);
          border-radius: 4rpx 4rpx 4rpx 4rpx;
          margin-left: 16rpx;
          display: flex;
          justify-content: center;
          align-items: center;
          font-size: 24rpx;
          font-family: PingFang SC, PingFang SC;
          font-weight: 400;
          color: #ff8a4c;
        }
        .label1 {
          background: rgba(201, 153, 255, 0.2);
          color: #c999ff;
        }
        .right_action {
          position: absolute;
          right: 0;
          height: 40rpx;
          width: 40rpx;
          display: flex;
          align-items: center;
          justify-content: center;
          .operate {
            position: absolute;
            right: 0;
            top: 50rpx;
            width: 240rpx;
            // height: 419rpx;
            background: #ffffff;
            box-shadow: 0rpx 5rpx 20rpx 0rpx rgba(0, 0, 0, 0.1);
            border-radius: 8rpx 8rpx 8rpx 8rpx;
            padding: 0 30rpx;
            box-sizing: border-box;
            background: #fff !important;
            z-index: 99999;

            .operate_item {
              width: 100%;
              border-bottom: 1rpx solid #eee;
              padding: 30rpx 0;
              box-sizing: border-box;
              display: flex;
              justify-content: center;
              font-size: 30rpx;
              font-family: PingFang SC, PingFang SC;
              font-weight: 400;
              color: #444444;
            }
          }
        }
      }
      .head_right1 {
        color: #eeeeee;
      }
    }
    .item_bom {
      width: 100%;
      margin-top: 8rpx;
      display: flex;
      padding-bottom: 32rpx;
      box-sizing: border-box;
      z-index: 99;
      position: relative;
      .bom_left {
        width: 48rpx;
        height: 56rpx;
        font-size: 24rpx;
        font-family: PingFang SC-Medium, PingFang SC;
        color: #888888;
        display: flex;
        justify-content: center;
      }
      .bom_left1 {
        color: #eeeeee;
      }
      .bom_right {
        flex: 1;
        display: flex;
        align-items: center;
        padding-left: 30rpx;
        padding-bottom: 32rpx;
        box-sizing: border-box;
        border-bottom: 1rpx solid #eee;
        image {
          width: 56rpx;
          height: 56rpx;
          border-radius: 50%;
        }
        text {
          margin-left: 16rpx;
          font-size: 24rpx;
          font-weight: 400;
          color: #888888;
        }
      }
      .bom_right1 {
        text {
          color: #eeeeee;
        }
        .bom_right_time {
          color: #888888;
        }
      }
    }
  }
  .bottom {
    width: 100%;
    height: 112rpx;
    background: #272727;
    position: fixed;
    bottom: 0;
    left: 0;
    padding: 16rpx 30rpx;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .bottom_left {
      display: flex;
      align-items: center;
      .item_left_click {
        width: 40rpx;
        height: 40rpx;
      }
      .bottom_left_text {
        margin-left: 16rpx;
        color: #eeeeee;
        font-size: 30rpx;
      }
    }

    .bottom_right {
      width: 250rpx;
      height: 80rpx;
      background: #ee6060;
      border-radius: 48rpx;
      text-align: center;
      line-height: 80rpx;
      font-size: 30rpx;
      color: #eeeeee;
    }
  }
  /deep/.u-modal__content__text {
    text-align: center;
  }
  .empty_view {
    margin-top: 300rpx;
    image {
      margin: 0 auto;
      width: 410rpx;
      // height: 410rpx;\
      height: 410rpx;
    }
    view {
      font-size: 30rpx;
      color: #d8d8d8;
      text-align: center;
    }
  }
}
</style>